package com.jang.service.cases;

import com.jang.Entity.ResponseEntity;
import com.jang.Entity.cases.CaseRuningEntity;
import com.jang.Entity.cases.ConfigEntity;
import com.jang.mapper.cases.CaseRuningMapper;
import com.jang.mapper.cases.ConfigMapper;
import com.jang.utils.HttpUtil;
import com.jang.utils.ResultHandle;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Component
@Service
public class CaseRuningServiceImpl implements CaseRuningService {
    @Autowired
    private ConfigMapper configMapper;
    @Autowired
    private CaseRuningMapper caseRuningMapper;

    @Override
    public String CaseRuning_Post(int case_id,int env_id){
        CaseRuningEntity caseRuningEntity=caseRuningMapper.getApiCaseById(String.valueOf(case_id));
        ConfigEntity configEntity = configMapper.getEnvConfigById(String.valueOf(env_id));
        caseRuningEntity.setUrl(configEntity.getEnv_addr()+caseRuningEntity.getUrl());
        List<ConfigEntity> list = configMapper.getHeaderConfig();
        caseRuningEntity.setConfig_list(list);
        List<String> resultList = new ArrayList();
        String newbody="";

        //获取前置接口
        if (caseRuningEntity.getPre_flag()==1){
            List<ConfigEntity> pre_case_list=caseRuningMapper.getPreApiById(String.valueOf(case_id));
            for (int i=0;i<pre_case_list.size();i++) {
                String pre_Result = this.PreRuning(pre_case_list.get(i).getPre_id(),env_id);
                resultList.add(pre_Result);
            }
            //根据前置结果改造请求头和body
            for (int j=0;j<resultList.size();j++){
                List<ConfigEntity> config_list= ResultHandle.handerReplace(caseRuningEntity.getConfig_list(),resultList.get(j));
                caseRuningEntity.setConfig_list(config_list);
                if (caseRuningEntity.getBody().contains("\"@")){
                    newbody=ResultHandle.bodyReplace(caseRuningEntity.getBody(),resultList.get(j));
                    caseRuningEntity.setBody(newbody);
                }
            }
            String result =HttpUtil.Post(caseRuningEntity);
            return result;
        }else {
            String result =HttpUtil.Post(caseRuningEntity);
            return result;
        }
    }
    @Override
    public String PreRuning(int case_id,int env_id){
        //获取当前接口配置
        CaseRuningEntity caseRuningEntity=caseRuningMapper.getApiCaseById(String.valueOf(case_id));
        ConfigEntity configEntity = configMapper.getEnvConfigById(String.valueOf(env_id));
        caseRuningEntity.setUrl(configEntity.getEnv_addr()+caseRuningEntity.getUrl());
        List<ConfigEntity> list = configMapper.getHeaderConfig();
        caseRuningEntity.setConfig_list(list);
        List<String> resultList = new ArrayList();
        //判断是否有前置接口
        if (caseRuningEntity.getPre_flag()==1){
            List<ConfigEntity> pre_case_list=caseRuningMapper.getPreApiById(String.valueOf(case_id));
            for (int i=0;i<pre_case_list.size();i++) {
                String pre_Result = PreRuning(pre_case_list.get(i).getPre_id(),env_id);
                resultList.add(pre_Result);
            }
            //根据前置结果改造请求头和body
            String newbody="";
            for (int j=0;j<resultList.size();j++){
                List<ConfigEntity>config_list= ResultHandle.handerReplace(caseRuningEntity.getConfig_list(),resultList.get(j));
                caseRuningEntity.setConfig_list(config_list);
                newbody=ResultHandle.bodyReplace(caseRuningEntity.getBody(),resultList.get(j));
            }
            caseRuningEntity.setBody(newbody);
            String result =HttpUtil.Post(caseRuningEntity);
            System.out.println(result);
            return result;
        }else {
            String result =HttpUtil.Post(caseRuningEntity);
            System.out.println(result);
            return result;
        }
    }

    @Override
    public Object CaseList(CaseRuningEntity caseRuningEntity){
        List<CaseRuningEntity> list=caseRuningMapper.findCaseEntityList(caseRuningEntity);
        ResponseEntity responseEntity = ResultHandle.returnWeb(list.size());
        responseEntity.setData(list);
        responseEntity.setCount(list.size()+1);
        return responseEntity;
    }

}
